<?php

namespace App\Http\Controllers\Table;

use App\Http\Controllers\BaseController;
use App\Models\Common\TableExtFeild;
use App\Models\Common\TableExtFeildBase;
use App\Services\Common\TableExtFeild as CommonTableExtFeild;
use Illuminate\Http\Request;

/**
 * 表头字段管理
 */
class TableExtFeildController extends BaseController
{

    /**
     * 获取表头列表
     */
    public function getlist(Request $request)
    {
        $list = TableExtFeildBase::where(['pid' => 0])->select(['tabletitle', 'tablename'])->get()->toArray();
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['list' => $list]]);
    }
    /**
     * 获取扩展字段信息
     */
    public function getinfo(Request $request)
    {
        $tablename = $request->tablename ?? '';
        $baseinfo = TableExtFeildBase::where(['tablename' => $tablename])->first();
        if (!$baseinfo) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $baseinfo->table_field = json_decode($baseinfo->table_field, true);
        $subinfo = TableExtFeildBase::where(['pid' => $baseinfo->id])->first();

        if ($subinfo) {
            $subinfo->table_field = json_decode($subinfo->table_field, true);
            $baseinfo->subinfo = $subinfo;
        }


        $feildinfo = TableExtFeild::where(['tablename' => $tablename, 'cpid' => $this->mangerinfo['cpid']])->first();
        if (!$feildinfo) {
            $pid = TableExtFeild::create(['cpid' => $this->mangerinfo['cpid'], 'tablename' => $tablename, 'pid' => $baseinfo->pid])->id;
            if ($subinfo) {
                TableExtFeild::create(['cpid' => $this->mangerinfo['cpid'], 'tablename' => $subinfo->tablename, 'pid' => $pid]);
            }
        }
        $feildinfo = TableExtFeild::where(['tablename' => $tablename, 'cpid' => $this->mangerinfo['cpid']])->first();
        if ($subinfo) {
            $feildinfo->subinfo = TableExtFeild::where(['tablename' => $subinfo->tablename, 'cpid' => $this->mangerinfo['cpid']])->first();
        }


        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['baseinfo' => $baseinfo, 'feildinfo' => $feildinfo]]);
    }
    public function setfeildtitle(Request $request)
    {
        $tablename = $request->tablename ?? '';
        $info = TableExtFeildBase::where(['tablename' => $tablename])->first();
        $feildinfo = TableExtFeild::where(['tablename' => $tablename, 'cpid' => $this->mangerinfo['cpid']])->first();
        if (!$info || !$feildinfo) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        $params = [
            'field_1' => $request->field_1 ?? '',
            'field_2' => $request->field_2 ?? '',
            'field_3' => $request->field_3 ?? '',
            'field_4' => $request->field_4 ?? '',
            'field_5' => $request->field_5 ?? '',
            'field_6' => $request->field_6 ?? '',
            'field_7' => $request->field_7 ?? '',
            'field_8' => $request->field_8 ?? '',
            'field_9' => $request->field_9 ?? '',
            'field_10' => $request->field_10 ?? '',
        ];
        TableExtFeild::where(['tablename' => $tablename, 'cpid' => $this->mangerinfo['cpid']])->update($params);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }
}
